﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using System.IO;

namespace ViewRidgeAssistant
{

    class customer_view_parameters
    {
        public bool _id;
        public bool _name;
        public bool _area_code;
        public bool _local_number;
        public bool _street;
        public bool _city;
        public bool _state;
        public bool _zip;
        public bool _country;
        public bool _email;

        public void setCustomerSettings(bool id, bool name, bool area_code, bool local_number, bool street, bool city, bool state,
                                        bool zip, bool country, bool email)
        {
            this._id = id;
            this._name = name;
            this._area_code = area_code;
            this._local_number = local_number;
            this._street = street;
            this._city = city;
            this._state = state;
            this._zip = zip;
            this._country = country;
            this._email = email;
        }
    }

    class transaction_view_parameters
    {
        public bool _id;
        public bool _work_id;
        public bool _date_acq;
        public bool _acq_price;
        public bool _pur_date;
        public bool _sales_price;
        public bool _ask_price;
        public bool _customer_id;

        public bool _work_title;
        public bool _work_copy;
        public bool _customer_name;
        public bool _customer_phone;

        public void setTransactionSettings( bool id, bool work_id, bool date_acq, bool acq_price, bool pur_date,
                                            bool sales_price, bool ask_price, bool customer_id,
                                            bool work_title, bool work_copy, bool customer_name, bool customer_phone)
        {
            this._id = id;
            this._work_id = work_id;
            this._date_acq = date_acq;
            this._acq_price = acq_price;
            this._pur_date = pur_date;
            this._sales_price = sales_price;
            this._ask_price = ask_price;
            this._customer_id = customer_id;

            this._customer_name = customer_name;
            this._customer_phone = customer_phone;
            this._work_title = work_title;
            this._work_copy = work_copy;
        }
    }

    class work_view_parameters
    {
        public bool _id;
        public bool _artist_id;
        public bool _copy;
        public bool _title;
        public bool _desc;

        public bool _artist_name;

        public void setWorkSettings(   bool id, bool artist_id, bool copy, bool title, bool desc, bool artist_name)
        {
            this._id = id;
            this._artist_id = artist_id;
            this._copy = copy;
            this._title = title;
            this._desc = desc;
            this._artist_name = artist_name;
        }
    }

    class artist_view_parameters
    {
        public bool _id;
        public bool _nationality;
        public bool _birth_date;
        public bool _deceased_date;
        public bool _artist_name;

        public void setArtistSettings(bool id, bool nationality, bool birth_date, bool deceased_date, bool artist_name)
        {
            this._id = id;
            this._nationality = nationality;
            this._birth_date = birth_date;
            this._deceased_date = deceased_date;
            this._artist_name = artist_name;
        }
    }

    class interests_view_parameters
    {
        public bool _customer;
        public bool _artist;
    }
    
    class main_interface_parameters
    {
        public bool _show_tables_panel;
        public bool _show_actions_panel;
        public bool _show_gridcontrol_panel;
    }

    

    class user_settings
    {

        public user_settings()
        {
            this._settingsFromFile = new ArrayList();

            this._customer_table_fields = new ArrayList();
            this._artist_table_fields = new ArrayList();
            this._work_table_fields = new ArrayList();
            this._trans_table_fields = new ArrayList();
            this._int_table_fields = new ArrayList();

            this._work_table_addon_fields_art = new ArrayList();
            this._trans_table_addon_fields_art = new ArrayList();
            this._trans_table_addon_fields_cust = new ArrayList();
            this._trans_table_addon_fields_work = new ArrayList();

            this._artist = new artist_view_parameters();
            this._transaction = new transaction_view_parameters();
            this._work = new work_view_parameters();
            this._customer = new customer_view_parameters();
            this._interests = new interests_view_parameters();

            this._main_gui = new main_interface_parameters();

            //set default settings of data grid fields;
            //this.setDefaultSettings();
            if (!(this.readSettingsFromFile("settings.vrconf")))
            {
                this.setDefaultSettings();
                MessageBox.Show("Настройки сгенерированы автоматически по-умолчанию");
            }
            this.applySettings();
            this.setDBSettings(this._server, this._schema, this._login, this._password, this._security, this._win_auth);
        }

        public artist_view_parameters      _artist;
        public transaction_view_parameters _transaction;
        public work_view_parameters        _work;
        public customer_view_parameters    _customer;
        public interests_view_parameters   _interests;


        public main_interface_parameters   _main_gui;

        public ArrayList _settingsFromFile;

        public ArrayList _customer_table_fields;
        public ArrayList _artist_table_fields;
        public ArrayList _work_table_fields;
        public ArrayList _work_table_addon_fields_art;          //Данные о художнике для отображения при картине 
        public ArrayList _trans_table_fields;
        public ArrayList _trans_table_addon_fields_cust;        //Данные о клиенте для отображения в транзации
        public ArrayList _trans_table_addon_fields_art;         //Данные о художнике для отображения в транзации
        public ArrayList _trans_table_addon_fields_work;        //Данные о работе для отображения в транзации
        
        public ArrayList _int_table_fields;


        public string _server;
        public string _schema;
        public string _login;
        public string _password;
        public bool _security;
        public bool _win_auth;

        public void setNewSettings()
        {
            
        }

        public void setCustomerSettings(bool id, bool name, bool area_code, bool local_number, bool street, bool city,
                                        bool state, bool zip, bool country, bool email)
        {
            this._customer.setCustomerSettings(id, name, area_code, local_number, street, city, state, zip, country, email);
            this.applySettings();
        }

        public void setTransactionSettings(bool id, bool work_id, bool date_acq, bool acq_price, bool pur_date,
                                            bool sales_price, bool ask_price, bool customer_id,
                                            bool work_title, bool work_copy, bool customer_name, bool customer_phone)
        {
            this._transaction.setTransactionSettings(id, work_id, date_acq, acq_price, pur_date, sales_price, ask_price, customer_id,
                work_title, work_copy, customer_name, customer_phone);
            this.applySettings();
        }

        public void setWorkSettings(bool id, bool artist_id, bool copy, bool title, bool desc, bool artist_name)
        {
            this._work.setWorkSettings(id, artist_id, copy, title, desc, artist_name);
            this.applySettings();
        }

        public void setArtistSettings(bool id, bool nationality, bool birth_date, bool deceased_date, bool artist_name)
        {
            this._artist.setArtistSettings(id, nationality, birth_date, deceased_date, artist_name);
            this.applySettings();
        }

        public bool setDBSettings(String t_server, String t_schema, String t_user, String t_pass, bool psec, 
                                    bool win_auth)
        {
            string final_conn_string = "";
            if (!(win_auth))
            {
                //string final_conn_string = "";
                //Сервер...
                final_conn_string += "Data Source=";
                final_conn_string += t_server;
                final_conn_string += ";";
                //Схема...
                final_conn_string += "Initial Catalog=";
                final_conn_string += t_schema;
                final_conn_string += ";";
                //Безосность...
                final_conn_string += "Persist Security Info=";
                if (psec) { final_conn_string += "true"; }
                else { final_conn_string += "false"; };
                final_conn_string += ";";
                //Пользователь...
                final_conn_string += "User ID=";
                final_conn_string += t_user;
                final_conn_string += ";";
                //Пароль...
                final_conn_string += "Password=";
                final_conn_string += t_pass;
            }
            else
            {
                //Безосность...
                final_conn_string += "Persist Security Info=False;";
                final_conn_string += "Integrated Security=true;";
                //Схема...
                final_conn_string += "Initial Catalog=";
                final_conn_string += t_schema;
                final_conn_string += ";";
                //Сервер
                final_conn_string += "Server=";
                final_conn_string += t_server;
            }

                if (!(Program.data_module.set_connection_settings(final_conn_string)))
                {
                    MessageBox.Show("Параметры указаны неверно");
                    return false;
                }
                //Сохранение настроек в user_set
                this._server = t_server;
                this._schema = t_schema;
                this._login = t_user;
                this._password = t_pass;
                this._security = psec;
                this._win_auth = win_auth;
                return true; 
        }

        public void applySettings()
        {
            //Сохранение параметров отображения клиентов
            this._customer_table_fields.Clear();

            if (this._customer._id)
            {
                this._customer_table_fields.Add("CustomerID");
                this._customer_table_fields.Add("Код клиента");
            }
            if (this._customer._name)
            {
                this._customer_table_fields.Add("Name");
                this._customer_table_fields.Add("ФИО");
            }
            if (this._customer._street)
            {
                this._customer_table_fields.Add("Street");
                this._customer_table_fields.Add("Улица");
            }
            if (this._customer._city)
            {
                this._customer_table_fields.Add("City");
                this._customer_table_fields.Add("Город");
            }
            if (this._customer._state)
            {
                this._customer_table_fields.Add("Region");
                this._customer_table_fields.Add("Регион");
            }
            if (this._customer._zip)
            {
                this._customer_table_fields.Add("ZipPostalCode");
                this._customer_table_fields.Add("Почт. индекс");
            }
            if (this._customer._country)
            {
                this._customer_table_fields.Add("Country");
                this._customer_table_fields.Add("Страна");
            }
            if (this._customer._area_code)
            {
                this._customer_table_fields.Add("AreaCode");
                this._customer_table_fields.Add("Код региона");
            }
            if (this._customer._local_number)
            {
                this._customer_table_fields.Add("PhoneNumber");
                this._customer_table_fields.Add("Телефон");
            }
            if (this._customer._email)
            {
                this._customer_table_fields.Add("Email");
                this._customer_table_fields.Add("Эл. почта");
            }
            if (this._customer._local_number)
            {
                this._customer_table_fields.Add("HouseNumber");
                this._customer_table_fields.Add("Дом");
            }
            //Сохранение параметров отображения художников
            this._artist_table_fields.Clear();
            if (this._artist._id)
            {
                this._artist_table_fields.Add("ArtistID");
                this._artist_table_fields.Add("Код художника");
            }
            if (this._artist._artist_name)
            {
                this._artist_table_fields.Add("Name");
                this._artist_table_fields.Add("ФИО");
            }
            if (this._artist._birth_date)
            {
                this._artist_table_fields.Add("BirthYear");
                this._artist_table_fields.Add("Год рождения");
            }
            if (this._artist._deceased_date)
            {
                this._artist_table_fields.Add("DeceaseYear");
                this._artist_table_fields.Add("Год смерти");
            }
            if (this._artist._nationality)
            {
                this._artist_table_fields.Add("Nationality");
                this._artist_table_fields.Add("Национальность");
            }

            //Сохранение параметров отображения картин
            this._work_table_fields.Clear();
            if (this._work._id)
            {
                this._work_table_fields.Add("WorkID");
                this._work_table_fields.Add("Код работы");
            }
            if (this._work._artist_id)
            {
                this._work_table_fields.Add("ArtistID");
                this._work_table_fields.Add("Код художника");
            }
            if (this._work._title)
            {
                this._work_table_fields.Add("Title");
                this._work_table_fields.Add("Название");
            }
            if (this._work._copy)
            {
                this._work_table_fields.Add("Copy");
                this._work_table_fields.Add("Копия");
            }
            if (this._work._desc)
            {
                this._work_table_fields.Add("Description");
                this._work_table_fields.Add("Описание");
            }
            if (this._work._artist_name)
            {
                this._work_table_fields.Add("ArtistName");
                this._work_table_fields.Add("Имя художника");
            }

            //Сохранение параметров отображения транзакций (сделок)
            this._trans_table_fields.Clear();
            if (this._transaction._id)
            {
                this._trans_table_fields.Add("TransactionID");
                this._trans_table_fields.Add("Код сделки");
            }
            if (this._transaction._work_id)
            {
                this._trans_table_fields.Add("WorkID");
                this._trans_table_fields.Add("Код работы");
            }
            if (this._transaction._customer_id)
            {
                this._trans_table_fields.Add("CustomerID");
                this._trans_table_fields.Add("Код клиента");
            }
            if (this._transaction._date_acq)
            {
                this._trans_table_fields.Add("DateAcquired");
                this._trans_table_fields.Add("Дата приобретения");
            } //(!!!)date !!!FIXME !!!TODO
            if (this._transaction._acq_price)
            {
                this._trans_table_fields.Add("AcquisitionPrice");
                this._trans_table_fields.Add("Цена приобретения");
            }
            if (this._transaction._pur_date)
            {
                this._trans_table_fields.Add("PurchaseDate");
                this._trans_table_fields.Add("Дата продажи");
            }
            if (this._transaction._sales_price)
            {
                this._trans_table_fields.Add("SalesPrice");
                this._trans_table_fields.Add("Цена продажи");
            }
            if (this._transaction._ask_price)
            {
                this._trans_table_fields.Add("AskingPrice");
                this._trans_table_fields.Add("Заявленная цена");
            }

            if (this._transaction._customer_name)
            {
                this._trans_table_fields.Add("CustomerName");
                this._trans_table_fields.Add("Клиент");
            }
            if (this._transaction._customer_phone)
            {
                this._trans_table_fields.Add("CustomerPhoneNumber");
                this._trans_table_fields.Add("Телефон");
            }
            if (this._transaction._work_title)
            {
                this._trans_table_fields.Add("WorkTitle");
                this._trans_table_fields.Add("Работа");
            }
            if (this._transaction._work_copy)
            {
                this._trans_table_fields.Add("WorkCopy");
                this._trans_table_fields.Add("Копия");
            }

            //Сохранение параметров отображения картин
            this._int_table_fields.Clear();
            if (this._interests._customer)
            {
                this._int_table_fields.Add("Customer");
                this._int_table_fields.Add("Клиент");
            }
            if (this._interests._artist)
            {
                this._int_table_fields.Add("Artist");
                this._int_table_fields.Add("Художник");
            }
        }

        public void setDefaultSettings()
        {
            //so, all of this settings switch on for presentation all information about this elements
            //sorry :(

            //this.readSettingsFromFile("I:\\settings.vrconf");

            //connection settings
            this._server = "VIRT-PC";
            this._schema = "vrdb";
            this._login = "sa";
            this._password = "server";
            this._security = true;
            this._win_auth = true;

            //show all customer's fields
            this._customer._id = false;
            this._customer._country = true;
            this._customer._email = true;
            this._customer._city = true;
            this._customer._local_number = true;
            this._customer._name = true;
            this._customer._state = true;
            this._customer._street = true;
            this._customer._zip = true;
            this._customer._area_code = true;

            //show all work fields
            this._work._artist_id = false;
            this._work._copy = true;
            this._work._id = false;
            this._work._title = true;
            this._work._desc = true;
            this._work._artist_name = true;

            //show all transaction fields
            this._transaction._acq_price = true;
            this._transaction._ask_price = true;
            this._transaction._customer_id = false;
            this._transaction._date_acq = true;
            this._transaction._id = false;
            this._transaction._pur_date = true;
            this._transaction._sales_price = true;
            this._transaction._work_id = true;

            this._transaction._work_title = true;
            this._transaction._work_copy = true;
            this._transaction._customer_name = true;
            this._transaction._customer_phone = true;
            
            //show all artist's fields
            this._artist._artist_name = true;
            this._artist._birth_date = true;
            this._artist._deceased_date = true;
            this._artist._id = false;
            this._artist._nationality = true;

            //show all interests fields
            this._interests._artist = true;
            this._interests._customer = true;

            //main gui (panels) settings
            this._main_gui._show_actions_panel = true;
            this._main_gui._show_tables_panel = true;
            this._main_gui._show_gridcontrol_panel = true;
        }

        public bool readSettingsFromFile(String setFile)
        {
            if (File.Exists(setFile))
            {
                this._settingsFromFile.Clear();
                StreamReader inFile = new StreamReader(setFile);
                String line = "";
                while((line = inFile.ReadLine()) != null)
                {
                    if (line.Length != 0)
                    if (line[0] != '#') //ignore comments in settings (config) file
                    {
                        this._settingsFromFile.Add(line);
                        //MessageBox.Show(line);
                    }
                }
                inFile.Close();
                //parsing data from settings file...
                String param_name = ""; 
                String param_value = "";
                bool b_param_value = true;
                foreach (String conf in this._settingsFromFile)
                {
                    param_name = conf.Substring(0, conf.IndexOf(" ")).Trim();
                    param_value = conf.Substring(conf.IndexOf(" ")).Trim();
                    //MessageBox.Show(param_name + " = " + param_value);
                    if ((param_value == "0") || (param_value == "1") || (param_value.ToLower() == "true") || (param_value.ToLower() == "false")) //this is logical value (true or false)
                    {
                        switch (param_value)
                        {
                            case "0":
                            case "False":
                            case "false":
                            case "FALSE":
                                b_param_value = false;
                                break;
                            case "1":
                            case "True":
                            case "true":
                            case "TRUE":
                                b_param_value = true;
                                break;

                        }
                        switch (param_name)
                        {
                            //db settings
                            case "db_pers_sec":
                                this._security = b_param_value;
                                break;
                            case "db_win_auth":
                                this._win_auth = b_param_value;
                                break;
                            //customer settings;
                            case "customer_id":
                                this._customer._id = b_param_value;
                                break;
                            case "customer_name":
                                this._customer._name = b_param_value;
                                break;
                            case "customer_area_code":
                                this._customer._area_code = b_param_value;
                                break;
                            case "customer_local_number":
                                this._customer._local_number = b_param_value;
                                break;
                            case "customer_street":
                                this._customer._street = b_param_value;
                                break;
                            case "customer_city":
                                this._customer._city = b_param_value;
                                break;
                            case "customer_state":
                                this._customer._state = b_param_value;
                                break;
                            case "customer_zip":
                                this._customer._zip = b_param_value;
                                break;
                            case "customer_country":
                                this._customer._country = b_param_value;
                                break;
                            case "customer_email":
                                this._customer._email = b_param_value;
                                break;
                            //transaction settings
                            case "transaction_id":
                                this._transaction._id = b_param_value;
                                break;
                            case "transaction_work_id":
                                this._transaction._work_id = b_param_value;
                                break;
                            case "transaction_date_acq":
                                this._transaction._date_acq = b_param_value;
                                break;
                            case "transaction_acq_price":
                                this._transaction._acq_price = b_param_value;
                                break;
                            case "transaction_pur_date":
                                this._transaction._pur_date = b_param_value;
                                break;
                            case "transaction_sales_price":
                                this._transaction._sales_price = b_param_value;
                                break;
                            case "transaction_ask_price":
                                this._transaction._ask_price = b_param_value;
                                break;
                            case "transaction_customer_id":
                                this._transaction._customer_id = b_param_value;
                                break;
                            case "transaction_work_title":
                                this._transaction._work_title = b_param_value;
                                break;
                            case "transaction_work_copy":
                                this._transaction._work_copy = b_param_value;
                                break;
                            case "transaction_customer_name":
                                this._transaction._customer_name = b_param_value;
                                break;
                            case "transaction_customer_phone":
                                this._transaction._customer_phone = b_param_value;
                                break;
                            //work settings
                            case "work_id":
                                this._work._id = b_param_value;
                                break;
                            case "work_artist_id":
                                this._work._artist_id = b_param_value;
                                break;
                            case "work_copy":
                                this._work._copy = b_param_value;
                                break;
                            case "work_title":
                                this._work._title = b_param_value;
                                break;
                            case "work_desc":
                                this._work._desc = b_param_value;
                                break;
                            case "work_artist_name":
                                this._work._artist_name = b_param_value;
                                break;
                            //artist settings
                            case "artist_id":
                                this._artist._id = b_param_value;
                                break;
                            case "artist_nationality":
                                this._artist._nationality = b_param_value;
                                break;
                            case "artist_birth_date":
                                this._artist._birth_date = b_param_value;
                                break;
                            case "artist_deceased_date":
                                this._artist._deceased_date = b_param_value;
                                break;
                            case "artist_artist_name":
                                this._artist._artist_name = b_param_value;
                                break;
                            case "show_actions_panel":
                                this._main_gui._show_actions_panel = b_param_value;
                                break;
                            case "show_gridcontrol_panel":
                                this._main_gui._show_gridcontrol_panel = b_param_value;
                                break;
                            case "show_tables_panel":
                                this._main_gui._show_tables_panel = b_param_value;
                                break;
                            //view customers interests parameters
                            case "customer":
                                this._interests._customer = b_param_value;
                                break;
                            case "artist":
                                this._interests._artist = b_param_value;
                                break;

                        }
                    }
                    else // this is not logical value (example "192.168.0.1" or "Local User")
                    {
                        switch (param_name)
                        {
                            case "db_server":
                                this._server = param_value;
                                break;
                            case "db_schema":
                                this._schema = param_value;
                                break;
                            case "db_user":
                                this._login = param_value;
                                break;
                            case "db_pass":
                                this._password = param_value;
                                break;
                        }
                    }
                }
                this._settingsFromFile.Clear();
                return true;
            }
            MessageBox.Show("Файл конфигурации приложения не найден!");
            return false;
        }

        public void saveSettingsToFile()
        {
            String[] sets = {"db_pers_sec " + this._security.ToString(),
                             "db_win_auth " + this._win_auth.ToString(),
                             "customer_id " + this._customer._id.ToString(),
                             "customer_name " + this._customer._name.ToString(),
                             "customer_area_code " + this._customer._area_code.ToString(),
                             "customer_local_number " + this._customer._local_number.ToString(),
                             "customer_street " + this._customer._street.ToString(),
                             "customer_city " + this._customer._city.ToString(),
                             "customer_state " + this._customer._state.ToString(),
                             "customer_zip " + this._customer._zip.ToString(),
                             "customer_country " + this._customer._country.ToString(),
                             "customer_email " + this._customer._email.ToString(),
                             "transaction_id " + this._transaction._id.ToString(),
                             "transaction_work_id " + this._transaction._work_id.ToString(),
                             "transaction_date_acq " + this._transaction._date_acq.ToString(),
                             "transaction_acq_price " + this._transaction._acq_price.ToString(),
                             "transaction_pur_date " + this._transaction._pur_date.ToString(),
                             "transaction_sales_price " + this._transaction._sales_price.ToString(),
                             "transaction_ask_price " + this._transaction._ask_price.ToString(),
                             "transaction_customer_id " + this._transaction._customer_id.ToString(),
                             "transaction_work_title " + this._transaction._work_title.ToString(),
                             "transaction_work_copy " + this._transaction._work_copy.ToString(),
                             "transaction_customer_name " + this._transaction._customer_name.ToString(),
                             "transaction_customer_phone " + this._transaction._customer_phone.ToString(),
                             "work_id " + this._work._id.ToString(),
                             "work_artist_id " + this._work._artist_id.ToString(),
                             "work_copy " + this._work._copy.ToString(),
                             "work_title " + this._work._title.ToString(),
                             "work_desc " + this._work._desc.ToString(),
                             "work_artist_name " + this._work._artist_name.ToString(),
                             "artist_id " + this._artist._id.ToString(),
                             "artist_nationality " + this._artist._nationality.ToString(),
                             "artist_birth_date " + this._artist._birth_date.ToString(),
                             "artist_deceased_date " + this._artist._deceased_date.ToString(),
                             "artist_artist_name " + this._artist._artist_name.ToString(),
                             "show_actions_panel " + this._main_gui._show_actions_panel.ToString(),
                             "show_gridcontrol_panel " + this._main_gui._show_gridcontrol_panel.ToString(),
                             "show_tables_panel " + this._main_gui._show_tables_panel.ToString(),
                             "customer " + this._interests._customer.ToString(),
                             "artist " + this._interests._artist.ToString(),
                             "db_server " + this._server.ToString(),
                             "db_schema " + this._schema.ToString(),
                             "db_user " + this._login.ToString(),
                             "db_pass " + this._password.ToString()
                            };
            System.IO.File.WriteAllLines(@"settings.vrconf", sets);                  
        }

    }
}
